package com.link.play.fit.ai.ui.guide


import android.Manifest
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.link.play.fit.ai.databinding.FgNotificationBinding
import com.link.play.fit.ai.ext.checkPermissions
import com.link.play.fit.ai.ui.guide.ac.GuidePbAc
import eightbitlab.com.blurview.BlurAlgorithm
import eightbitlab.com.blurview.RenderEffectBlur
import eightbitlab.com.blurview.RenderScriptBlur
import kotlinx.coroutines.launch

class NotificationFg : BasePageFragment<FgNotificationBinding>(FgNotificationBinding::inflate) {


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        binding.blurView.setupWith(binding.fl, getBlurAlgorithm())
            .setOverlayColor(Color.parseColor("#82000000"))
            .setBlurAutoUpdate(true)
            .setBlurRadius(2f)
    }

    override fun onResumeOneShoot() {
        super.onResumeOneShoot()

        val activity = requireActivity() as GuidePbAc
        if (activity.currentIndex == pageIndex) {
            lifecycleScope.launch {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    Log.e(TAG, "POST_NOTIFICATIONS")
                    val map = activity.checkPermissions(Manifest.permission.POST_NOTIFICATIONS)
                    Log.e(TAG, "$map ${activity.currentIndex} pageIndex=$pageIndex")
                    activity.binding.tvNext.callOnClick()
                } else {
                    Log.e(TAG, "POST_NOTIFICATIONS NO")
                }
            }
        }
    }


    private fun getBlurAlgorithm(): BlurAlgorithm {
        val algorithm = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            RenderEffectBlur()
        } else {
            RenderScriptBlur(requireContext())
        }
        return algorithm
    }

}